<?php

namespace App\Admin\Actions;

use App\Exceptions\ApiException;
use App\Interfaces\WorkshopInterFace;
use App\model\Workshop;
use App\UserShenhe;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class UserShenheRefuse extends RowAction
{
    public $name = '拒绝';
    public function authorize($user, $model)
    {
        if ($model->status==UserShenhe::STATUS_SHENHE){
            return true;
        }
        return false;
    }
    public function handle(Model $model,Request $request)
    {        // $model ...
        $userShenhe=$model;
        if ($userShenhe->status==UserShenhe::STATUS_JUJUE){
            return $this->response()->error('已经审核过')->refresh();
        }
        $workshopInterFace=app(WorkshopInterFace::class);
        $user=\App\User::where('role_id','=',2)->first();
        if (!$user){
            return $this->response()->error('请设置一个推广员')->refresh();
        }
        switch ($userShenhe->type){
            case UserShenhe::Created_panyuan:
                $workshop=Workshop::withoutGlobalScope('shenhe')->find($userShenhe->workshop_id);


                $workshopInterFace->diskSourceRefuse($user,$workshop,$userShenhe,$request->refuse);
                break;
            case UserShenhe::View_panyuan:
                $workshop=Workshop::withoutGlobalScope('shenhe')->find($userShenhe->workshop_id);
                $workshopInterFace->viewSourceRefuse($user,$workshop,$userShenhe,$request->refuse);
                break;
            case  UserShenhe::Updated_panyuan:
                $workshop=Workshop::withoutGlobalScope('shenhe')->find($userShenhe->workshop_id);
                $workshopInterFace->updateSourceRefuse($user,$workshop,$userShenhe,$request->refuse);
                break;
        }

        return $this->response()->success('拒绝成功')->refresh();
    }
    public function form()
    {

        $this->textarea('refuse', '拒绝原因')->rules('required');
    }
}
